package com.example.springbootjdbc.dao.impl;

import com.example.springbootjdbc.dao.UsersDao;
import com.example.springbootjdbc.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName UsersDaoImpl
 * @Description UsersDaoImpl 持久层实现类
 * @Author ZX
 * @Date 2020/5/26
 */
@Repository
public class UsersDaoImpl implements UsersDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * @Description: 添加用户
     * @Param: users
     * @return: 
     */
    @Override
    public void insertUsers(Users users) {
        String sql = "insert users(username,age,phone,email) values(?,?,?,?)";
        this.jdbcTemplate.update(sql,users.getUsername(),users.getAge(),users.getPhone(),users.getEmail());
    }

    /**
     * @Description: 查找用户
     * @Param: users
     * @return:
     */
    @Override
    public Users selectUserById(Integer id) {
        //使用的函数不能传回对象，所以在外面定义。
        Users users = new Users();
        String sql = "select * from users where id = ?";
        //方法二
        //存储？中对象
        Object[] arr = new Object[]{id};
        this.jdbcTemplate.query(sql,arr, new RowCallbackHandler() {

            /**
             * @Description: processRow
             * @Param: [resultSet]
             * @Return: void
             */
            @Override
            public void processRow(ResultSet resultSet) throws SQLException {
                users.setId(resultSet.getInt("id"));
                users.setAge(resultSet.getInt("age"));
                users.setPhone(resultSet.getInt("phone"));
                users.setUsername(resultSet.getString("username"));
                users.setEmail(resultSet.getString("email"));
            }
        });

//        方法一
//        this.jdbcTemplate.query(sql, new RowCallbackHandler() {
//
//            /**
//             * @Description: processRow
//             * @Param: [resultSet]
//             * @Return: void
//             */
//            @Override
//            public void processRow(ResultSet resultSet) throws SQLException {
//                users.setId(resultSet.getInt("id"));
//                users.setAge(resultSet.getInt("age"));
//                users.setPhone(resultSet.getInt("phone"));
//                users.setUsername(resultSet.getString("username"));
//                users.setEmail(resultSet.getString("email"));
//            }
//        });
        return users;
    }


    public List<Users> selectUsersAll(){
        String sql = "select * from users";

        return this.jdbcTemplate.query(sql, new RowMapper<Users>() {

            /**
             * @Description: 结果集的映射
             * @Param: [resultSet, i]
             * @Return: com.example.springbootjdbc.pojo.Users
             */
            @Override
            public Users mapRow(ResultSet resultSet, int i) throws SQLException {
                Users users = new Users();
                users.setId(resultSet.getInt("id"));
                users.setAge(resultSet.getInt("age"));
                users.setPhone(resultSet.getInt("phone"));
                users.setUsername(resultSet.getString("username"));
                users.setEmail(resultSet.getString("email"));
                return users;
            }
        });
    }

}
